/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package tRM_RTNB.impl;

import java.util.Collection;

import node1.AbstractTask;
import node1.impl.Node1Impl;

import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;
import resultsWCRT.ResultsWCRT;
import resultsWCRT.ResultsWCRTFactory;
import resultsWCRT.Scheduling;
import tRM_RTNB.TRM_RTNB;
import tRM_RTNB.TRM_RTNBPackage;
import tRM_RTNB.Task;
import utils.UtilsMaths;
import utils.UtilsModelInstance;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>TRM RTNB</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link tRM_RTNB.impl.TRM_RTNBImpl#getTasks <em>Tasks</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class TRM_RTNBImpl extends Node1Impl implements TRM_RTNB {
	/**
	 * The cached value of the '{@link #getTasks() <em>Tasks</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTasks()
	 * @generated
	 * @ordered
	 */
	protected EList<Task> tasks;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TRM_RTNBImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return TRM_RTNBPackage.Literals.TRM_RTNB;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Task> getTasks() {
		if (tasks == null) {
			tasks = new EObjectContainmentEList<Task>(Task.class, this, TRM_RTNBPackage.TRM_RTNB__TASKS);
		}
		return tasks;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case TRM_RTNBPackage.TRM_RTNB__TASKS:
				return ((InternalEList<?>)getTasks()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case TRM_RTNBPackage.TRM_RTNB__TASKS:
				return getTasks();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case TRM_RTNBPackage.TRM_RTNB__TASKS:
				getTasks().clear();
				getTasks().addAll((Collection<? extends Task>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case TRM_RTNBPackage.TRM_RTNB__TASKS:
				getTasks().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case TRM_RTNBPackage.TRM_RTNB__TASKS:
				return tasks != null && !tasks.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void compute() {

		/**/
		if (!done){
			
			EList<AbstractTask> list = new BasicEList<AbstractTask>();
			for (Task t : this.tasks ) {
				list.add(t);
			}
			
			/**/
			ResultsWCRT rWCRT = UtilsModelInstance.createResultsWCRT();
			UtilsModelInstance.linkComputeBox(this, rWCRT, false);
			
			/* Order list by priority */
			UtilsModelInstance.orderByPriority(list);

						
			/**/
			for (int i = 0 ; i < list.size() ; i++) {

//				System.out.println(i);
				int a_0 = list.get(i).getWcet();
				int a_n = 0;
				/**/
				for (int j = 0 ; j < i ; j++) {

					a_0 = a_0 + list.get(j).getWcet();

				} // Loop j

				/**/
				while (a_n != a_0) {

					/**/
					a_0 = a_n;
					a_n = list.get(i).getWcet();

					/**/
					for (int j = 0 ; j < i ; j++) {

						a_n = a_n + UtilsMaths.ceiling(a_0, list.get(j).getPeriod()) * list.get(j).getWcet();

					} // Loop j

				}

				/**/
				resultsWCRT.Task lt = ResultsWCRTFactory.eINSTANCE.createTask();
				lt.setId(list.get(i).getId());
				lt.setWcrt(a_n);
				
				/**/
//				System.out.println(">> " + list.get(i).getDeadline());
				if (a_n <= list.get(i).getDeadline()) {

					lt.setScheduling(Scheduling.SCHEDULABLE);

				}

				/**/
				else {

					lt.setScheduling(Scheduling.NON_SCHEDULABLE);
				}
//				System.out.println("add result");
				/**/
				rWCRT.getTasks().add(lt);
			} // Loop i

			
			rWCRT.compute();
			this.done = true;

		}
		
	}

} //TRM_RTNBImpl
